package com.jak.demo.Thread;

import com.best.transport.context.SystemContextHolder;
import lombok.SneakyThrows;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author Jiang Akang
 * employeeId: BG435424
 * @date 2020/8/20
 **/
public class TestThreadPool1 {
    @SneakyThrows
    public static void main(String[] args) {
        //1.模拟一个任务
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        Future<Integer> future = executorService.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int sum = 0;
                for (int i = 0; i <= 100; i++) {
                    System.out.println(i);
                    sum += i;
                }
                return sum;
            }
        });
        Integer integer = future.get();
        System.out.println(integer);
        executorService.shutdown();
        //2.模拟十个任务
        ExecutorService pool = Executors.newFixedThreadPool(5);
        List<Future<Integer>> list = new ArrayList<>();
        for (int i = 0; i <= 10; i++) {
            Future<Integer> submit = pool.submit(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    int sum = 0;
                    for (int i = 0; i <= 100; i++) {
                        sum += i;
                        System.out.println(Thread.currentThread() + "," +  i);
                    }
                    return sum;
                }
            });
            list.add(submit);
        }
        pool.shutdown();
        for (Future<Integer> integerFuture : list) {
            integerFuture.get();
        }
    }
}
